/**
 * 
 */
package com.et114.modules.org.utility;

import javax.servlet.http.HttpSession;

import org.acegisecurity.ui.AbstractProcessingFilter;

import org.springframework.util.Assert;

import java.util.Date;

import com.et114.components.acegi.Constants;
import com.et114.components.acegi.intercept.web.ILoginSuccessfulAuthentication;

/**
 * @author guanhw
 */
public class CheckPassInterceptor implements ILoginSuccessfulAuthentication {
	private int deleyDay = 30 ; 
	private String failureUrl ; 
	private AbstractProcessingFilter authenticationProcessingFilter ;
	/* (non-Javadoc)
	 * @see com.et114.components.acegi.intercept.web.ILoginSuccessfulAuthentication#afterSuccessfulAuthentication(javax.servlet.http.HttpSession)
	 */
	public void afterSuccessfulAuthentication ( HttpSession session )
			throws Exception {
		// TODO Auto-generated method stub
		SessionUserInfo sessionUserInfo = ( SessionUserInfo ) session .getAttribute ( Constants.SESSION_CURRENT_USER_KEY );
		Date date = sessionUserInfo.getLastModPassTime ( ) ; 
		if ( date == null  ) 
			authenticationProcessingFilter.setDefaultTargetUrl ( failureUrl ) ; 
		else {
			long cut = System.currentTimeMillis ( ) ; 
			long lastTime = date.getTime ( ) ; 
			long deleyTime = 30 * 24 * 60 * 1000 ;
			if ( cut - lastTime > deleyTime ) 
				authenticationProcessingFilter.setDefaultTargetUrl ( failureUrl ) ; 
		}
	}

	public int getDeleyDay ( ) {
		return deleyDay;
	}

	public void setDeleyDay ( int deleyDay ) {
		this.deleyDay = deleyDay;
	}

	public AbstractProcessingFilter getAuthenticationProcessingFilter ( ) {
		return authenticationProcessingFilter;
	}

	public void setAuthenticationProcessingFilter (
			AbstractProcessingFilter authenticationProcessingFilter ) {
		this.authenticationProcessingFilter = authenticationProcessingFilter;
	}

	public String getFailureUrl ( ) {
		return failureUrl;
	}

	public void setFailureUrl ( String failureUrl ) {
		Assert.notNull ( failureUrl ) ; 
		this.failureUrl = failureUrl;
	}

}
